using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using System.Data;
using System.Windows.Forms;
namespace DnDDB{
	public class Account{
		string logon;
		string password;
	  	bool paidp;
		string email;

        public Account(string logon)
        {
            this.logon = logon;
            string dbCommand = "SELECT * FROM `Account` accnt WHERE " +
            "accnt.logon = '" + logon + "';";
            getMySQL get = new getMySQL();
            DataRow dr = get.GetMySQLFilledRow(dbCommand);
            // jeff - anything special I need to do here for the boolean value?
            this.password = dr["password"].ToString();
            this.paidp = Convert.ToBoolean(dr["paidp"]);
            this.email = dr["email"].ToString();
        }
		public Account(string logon, string passwd){
			this.logon = logon;
			password = passwd;
			string dbCommand = "SELECT * FROM `Account` accnt spell WHERE " +
			"accnt.logon = '" + logon + "' AND accnt.password = '"+ password +"';";
            getMySQL get = new getMySQL();
			DataRow dr = get.GetMySQLFilledRow(dbCommand);
			// jeff - anything special I need to do here for the boolean value?
			paidp = Convert.ToBoolean(dr["paidp"]);
			email = dr["email"].ToString();
		}
		
		public static bool validLogin(string logon, string passwd){
            getMySQL get = new getMySQL();
			DataTable da = get.GetMySQLFilledTable(
			    "SELECT * FROM `Account` WHERE logon='" + logon + "' AND `password` = '" + passwd + "';");
		    if (da.Rows.Count > 0){
				return true;
			} else{
				return false;
			}
		}
		
		public void save(){
            getMySQL get = new getMySQL();
			string dbcommand = "UPDATE `Account` SET " +
            " `password` = '" + this.password + "'," +
            " paidp = " + this.paidp.ToString() + "," +
            " email = '" + this.email + "' WHERE logon='" + logon + "' ;";
			try {
                get.GetMySQLExecuteNonQuery(dbcommand);
		    } catch (Exception e) {
			    Console.WriteLine(e);
                MessageBox.Show("account saving failed");
                return;
		    }
		}

        public static void save(string logon, string password, bool paidp, string email)
        {
            getMySQL get = new getMySQL();
            string dbcommand = "UPDATE `Account` SET " +
            " `password` = '" + password + "'," +
            " paidp = '" + paidp.ToString() + "'," +
            " email = '" + email + "' WHERE logon='" + logon + "' ;";
            try
            {
                get.GetMySQLExecuteNonQuery(dbcommand);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                MessageBox.Show("account saving failed, trying again");
                return;
            }
        }
		
		public void addCharacter(string name){
            getMySQL get = new getMySQL();
			string dbcommand = "INSERT INTO `Account_has_Character`(Account_logon, Character_nameCharacter) VALUES ('"
			+ logon + "', '" +
            name + "');";
			try {
                get.GetMySQLExecuteNonQuery(dbcommand);
		    } catch (Exception e) {
			    Console.WriteLine(e);
                MessageBox.Show("account saving failed, trying again");
                return;
		    }
		}
		
		public static void addAccount(string logon, string password, bool paidp, string email){
            getMySQL get = new getMySQL();
			string dbcommand = "INSERT INTO `Account`(logon, `password`, paidp, email) VALUES ('"
			+ logon + "', '" +
			password + "'," +
            paidp.ToString() + ",'" +
            email + "');";
			try {
                get.GetMySQLExecuteNonQuery(dbcommand);
		    } catch (Exception e) {
			    Console.WriteLine(e);
                MessageBox.Show("account saving failed, trying again");
                return;
		    }
		}
		
		
		
		public string getLogon(){
			return logon;
		}
		
		public void setLogon(string newlogon){
			logon = newlogon;
		}
		
		public string getPassword(){
			return password;
		}
		
		public void setPassword(string passwd){
			password = passwd;
		}
		
		public string getEmail(){
			return email;
		}
		
		public void setEmail(string newemail){
			email = newemail;
		}
		
		public bool getPaidp(){
			return paidp;
		}
		
		public void setPaid(bool paid){
			paidp = paid;
		}
	
		public List<string> getCharacter(){
			List<string> toReturn = new List<string>();
            getMySQL get = new getMySQL();
			DataTable da = get.GetMySQLFilledTable(
			    "SELECT * FROM `Account_has_Character` WHERE Account_logon='" + this.logon + "';");
			foreach (DataRow row in da.Rows) {
				toReturn.Add(row["Character_nameCharacter"].ToString());
			}
			return toReturn;
		}
		
	}


}
